from collections import OrderedDict


ATOMIC_PERMS = OrderedDict(
    (
        ("none", 0),
        #
        # User interactions
        #
        ("create_users", 0),
        ("delete_users", 0),
        ("change_perms", 0),
        ("reset_passwords", 0),
        ("list_users", 0),
        #
        # Hub interactions
        #
        ("link_hubs", 0),
        ("edit_hubs", 0),
        ("unlink_hubs", 0),
        ("list_hubs", 0),
    )
)

# All the atomic permissions in one number
FULL_PERMS = 1


def init_permission_system():
    global ATOMIC_PERMS, FULL_PERMS
    for k, name in enumerate(ATOMIC_PERMS.keys()):
        ATOMIC_PERMS[name] = 2**k
        FULL_PERMS |= ATOMIC_PERMS[name]
    print("Permission system was initialized successfully")


def add(initial_perm: int, perm_to_add: int):
    initial_perm |= perm_to_add


def toggle(initial_perm: int, atomic_perm_to_toggle: int):
    initial_perm ^= atomic_perm_to_toggle


def remove(initial_perm: int, perm_to_remove: int):
    initial_perm &= ~perm_to_remove


def has(initial_perm: int, perm_to_check: int) -> bool:
    return (initial_perm & perm_to_check) != 0
